from tools import ImageUtil, MoveUtil, CommonUtil, YoloV8Util


def go_door_from_mark(move_random_index, window, role_loc, dir_mark):
    # 尝试3次，以免当前界面没有敌人，敌人在界面之外时，一直循环次操作
    if move_random_index < 3:
        is_mark, mark_loc, file_name = ImageUtil.find_one_from_dir(window, dir_mark)
        if is_mark:
            offset = CommonUtil.get_offset_with_name(file_name)
            MoveUtil.moveTo(role_loc, (mark_loc[0] + offset[0], mark_loc[1] + offset[1]))
            return True
    return False


def find_door(data_dic, location, direction_key, door_key):
    """
    找门，并进入
    """
    direction_locs = YoloV8Util.get_data_all(data_dic, direction_key)
    if len(direction_locs) > 0:
        far_loc = CommonUtil.get_max_far_loc(location, direction_locs)
        MoveUtil.moveTo(location, far_loc)

        # 可以进门，查找门的坐标，进门
        door_locs = YoloV8Util.get_data_all(data_dic, door_key)
        print(f"door_locs===>>>{door_locs}")
        if len(door_locs) > 0:
            door = CommonUtil.get_min_near_loc(location, door_locs)
            if MoveUtil.is_need_enter(location, door):
                MoveUtil.moveTo(location, door)

        else:
            # 没有找到门
            MoveUtil.move_random(0.3)


def is_open(data_dic, direction_key):
    flag, loc = YoloV8Util.get_data_one(data_dic, direction_key)
    return flag, loc
